/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.command;

import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.utils.Debug;

/**
 * 
 * 
 * @author Remi SCHNEKENBURGER
 */
public class DeleteConnectorEndFromModelCommand extends Command {
	
	/**
	 * Property to delete.
	 */
	private ConnectorEnd child;
	
	/**
	 * 
	 */
	private Property property;
	
	/**
	 * 
	 */
	private ConnectableElement role;
	
	/**
	 * Owner of the property to delete.
	 */
	private Connector parent;
	
	/**
	 * Default Constructor.
	 * 
	 * @param child 
	 */
	public DeleteConnectorEndFromModelCommand(ConnectorEnd child) {
		if (child == null) {
			throw new IllegalArgumentException();
		}
		setLabel("Delete <Property>"+child);
		this.child = child;
		this.parent = (Connector)child.getOwner();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	/**
	 * 
	 */
	@Override
	public void execute() {
		Assert.isNotNull(parent, "DeleteProperty.child has no parent");
		Debug.debug(this, "DeletePropertyFromModel.execute -> "+child, 21);
		
		// delete property: owner = classifier or association or DataType
		child.eAdapters().clear();
		
		// save all refs.
		property=child.getPartWithPort();
		role=child.getRole();
		
		child.setPartWithPort(null);
		child.setRole(null);
		
		(parent).getEnds().remove(child);
		
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	/**
	 * 
	 */
	@Override
	public void redo() {
		execute();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	/**
	 * 
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void undo() {
		child.setPartWithPort(property);
		child.setRole(role);
		
		(parent).getEnds().add(child);
	}
}
